#include "HelloWorld.h"
using namespace std;
using namespace egg;

EggApp* app=0;
bool gKeyDown[256]={false};
bool gButtonDown[3]={false};
egg::uint32 gPreMouse[2]={0};
egg::uint32 gCurMouse[2]={0};
egg::uint32 CANVAS_WIDTH=1280;
egg::uint32 CANVAS_HEIGHT=720;

void render(void) {
    app->render();
    glutSwapBuffers();
}

void onMouseMove(int x,int y){
    app->onMouseMove(x,y);
    gPreMouse[0]=x;
    gPreMouse[1]=y;
}

void onMouseWheel(int button,int delta,int x,int y){
    app->onMouseWheel(button,delta,x,y);
}

void onMouseButton(int button,int state,int x,int y){
    if(state==GLUT_DOWN){
        gButtonDown[button]=true;
        app->onMouseDown(button,x,y);
    }else if(state==GLUT_UP){
        gButtonDown[button]=false;
        app->onMouseUp(button,x,y);
    }
}

void onKeyboard(unsigned char kc,int x,int y){
    app->onKeyboard(kc,x,y);
}

void init(){
    app=new HelloWorld();
    app->init();
}

int main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
    glutInitWindowSize(CANVAS_WIDTH,CANVAS_HEIGHT);
    glutCreateWindow("eggine");
    glutKeyboardFunc(onKeyboard);
    glutMouseFunc(onMouseButton);
    glutMouseWheelFunc(onMouseWheel);
    glutMotionFunc(onMouseMove);
    glutPassiveMotionFunc(onMouseMove);
    init();
    glutIdleFunc(render);
    glutMainLoop();
    return 0;
}